//
//  AppDelegate.swift
//  weibo08
//
//  Created by 陈杰明 on 16/2/26.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //
        let account = MyUserAccountViewModel.sharedInstance.userAccount
        print("account:\(account)")
        
        //设置导航栏item的颜色
        setupAppearance()
        
        //1.创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        let tarbarVC = MyMainViewController()
        
        //2.创建根控制器
        window?.rootViewController = defaultController()
        
        //ToDo 测试welcome页面
//        window?.rootViewController = MyNewFeatureViewController()
//        window?.rootViewController?.view.backgroundColor = UIColor.redColor()
        
        //3.设置为主窗口
        window?.makeKeyAndVisible()
       
        return true
    }
    //设置默认控制器
    private func defaultController() -> UIViewController {
        if !MyUserAccountViewModel.sharedInstance.userLogin {
            
        return MyMainViewController()
            
        }
        //用户已经登录
        return isNewVersion() ? MyNewFeatureViewController() : MyWelcomeViewController()
    }
    //目标控制器
    private func switchRootViewController(isMain: Bool) {
        
        window?.rootViewController = isMain ? MyMainViewController() : MyWelcomeViewController()
    }
    //类方法方便外面调用
    class func outSwitchRootViewCOntroller(isMain: Bool) {
    
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(isMain)
    }
    //判断是否是新的版本号
    func isNewVersion() -> Bool {
        
        //当前的版本号
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
    
        //上一次的版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
       let newVersion = version != sandboxVersion
        //是新版本保存起来
        if newVersion {
            //将当前保存起来
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxVersionKey)
            //同步
            NSUserDefaults.standardUserDefaults().synchronize()            
        }
        return newVersion
        
    }
    
    //设置外观颜色
    private func setupAppearance() {
        let item = UIBarButtonItem.appearance()
        item.tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

